from classes.node import Node


def traverse(n):
    if n is None:
        return
    print(n.value)
    traverse(n.left)
    traverse(n.right)


def level_order(node):
    """二叉树按层遍历并收集节点"""
    queue = [node]
    while queue:
        length = len(queue)
        for n in range(length):
            cur_node = queue.pop()
            print(cur_node.value, end=' ')
            if cur_node.left:
                queue.insert(0, cur_node.left)
            if cur_node.right:
                queue.insert(0, cur_node.right)
        print()


if __name__ == '__main__':
    head = Node(10)
    head.left = Node(5)
    head.right = Node(8)
    head.left.left = Node(3)
    head.left.right = Node(4)
    head.right.left = Node(6)

    # traverse(head)
    level_order(head)
